const express = require("express");
const app = express();

app.use(express.urlencoded());

// app.use((req, res, next) => {
//   if (req.headers["content-type"] === "application/x-www-form-urlencoded") {
//     req.body = { name: "jack", age: 20, gender: "boy" };
//   }
//   next();
// });

app.use(express.json());
// app.use((req, res, next) => {
//   if (req.headers["content-type"] === "application/json") {
//     req.body = 对象化(获取报文);
//   }
//   next();
// });

app.get("/users/:id", (req, res) => {
  console.log(req.params);
  res.send("hello users");
});

app.get("/users/:p1/:cid/:p2", (req, res) => {
  console.log(req.params);
  res.send("hello users");
});

app.get("/posts", (req, res) => {
  console.log(req.query);
  res.send("hello posts");
});

app.get("/comments/:id", (req, res) => {
  console.log(req.params);
  console.log(req.query);
  res.send("hello comments");
});

app.post("/users", (req, res) => {
  console.log(req.body);

  res.send("hello post users");
});

app.put("/users", (req, res) => {
  console.log(req.headers);
  res.send("hello put users");
});

app.listen(8080, () => {
  //服务器启动成功后执行
  console.log("http://127.0.0.1:8080");
});
